﻿/*
 * Created by SharpDevelop.
 * User: Lance
 * Date: 3/8/2011
 * Time: 11:01 AM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.IO;
using System.Collections.Generic;

namespace BatchTag
{
	class Program
	{
		public static void Main(string[] args)
		{
			Console.WriteLine("Add File name Tag to vm,svm files!");
			
			// TODO: Implement Functionality Here
			string directoty=Directory.GetCurrentDirectory();
			
			if(args.Length>1){
				directoty=args[1];
			}
			Console.WriteLine("Processing directory :"+directoty);
			
			string[] svmFiles= Directory.GetFiles(directoty,"*.svm",SearchOption.AllDirectories);

			string[] vmFiles= Directory.GetFiles(directoty,"*.vm",SearchOption.AllDirectories);	

			List<string> files=new List<string>();
			
			files.AddRange(svmFiles);

			files.AddRange(vmFiles);
			
			files.Sort();
			
			foreach( string file in files)
			{
				FileInfo fileInfo=new FileInfo(file);
				
				string startTag=string.Format("<!-- Start {0} -->\r\n",fileInfo.Name);

				string endTag=string.Format("\r\n<!-- End {0} -->",fileInfo.Name);
				
				string content=File.ReadAllText(file);
				
				content=content.Trim('\r','\n',' ');
				
				if(content.StartsWith("#macro",StringComparison.CurrentCultureIgnoreCase)
					   ||content.IndexOf("<!DOCTYPE",StringComparison.CurrentCultureIgnoreCase)>-1)
					continue;
				
				if(!content.StartsWith(startTag,StringComparison.CurrentCultureIgnoreCase)
				   ||!content.EndsWith(endTag,StringComparison.CurrentCultureIgnoreCase))
				   {
				   	 content= startTag+ content+ endTag;
				   	 File.WriteAllText(file,content);
					 Console.WriteLine("Processing file : "+file);
				   }
			}
			
			Console.Write("Process done, Press any key to continue . . . ");
			Console.ReadKey(true);
		}
	}
}